import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.*;

public class LoginWin extends JFrame implements ActionListener {
    JLabel label_Name, label_Password;
    JButton login, exit;
    JTextField name_field, password_field;
    InterfaceWin interfaceWin;
    Box baseBox, box1, box2, box3;
    Connection con;
    Statement sql;
    ResultSet rs;
    OperationWin operationWin;
    LoginWin(String title) {
        setTitle(title);
        label_Name = new JLabel("请输入账号");
        label_Password = new JLabel("请输入密码");
        login = new JButton("登录");
        exit = new JButton("退出");
        name_field = new JTextField(20);
        password_field = new JTextField(10);

        login.addActionListener(this);
        exit.addActionListener(this);

        setLayout(null);
        baseBox = Box.createHorizontalBox();
        box1 = Box.createVerticalBox();
        box2=Box.createVerticalBox();
        box1.add(label_Name);
        box1.add(Box.createVerticalStrut(30));
        box1.add(label_Password);
        box2.add(name_field);
        box2.add(Box.createVerticalStrut(10));
        box2.add(password_field);
        baseBox.add(box1);
        baseBox.add(Box.createHorizontalStrut(10));
        baseBox.add(box2);
        box3 = Box.createHorizontalBox();
        box3.add(login);
        box3.add(Box.createHorizontalStrut(50));
        box3.add(exit);
        add(baseBox);
        baseBox.setBounds(40,50,350,90);
        add(box3);
        box3.setBounds(130,150,350,50);

        setBounds(700, 300, 450, 250);
        setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    public void actionPerformed(ActionEvent event) {
        if (event.getSource() == login) {
            if(name_field.getText().trim().equals(""))
                JOptionPane.showMessageDialog(this,"账号不能为空","提示",JOptionPane.OK_OPTION);
            else if(password_field.getText().trim().equals(""))
                JOptionPane.showMessageDialog(this,"密码不能为空","提示",JOptionPane.OK_OPTION);
            else{
                try{
                    con= DriverManager.getConnection("jdbc:sqlserver://localhost:49689;DatabaseName=identity","sa","061210");
                    sql=con.createStatement();
                    rs=sql.executeQuery("SELECT * FROM Login WHERE LoginKeyword='"+password_field.getText().trim()+"'");
                    if(!rs.next())
                        JOptionPane.showMessageDialog(this,"密码错误","WARNING",JOptionPane.WARNING_MESSAGE);
                    else {
                        rs = sql.executeQuery("SELECT * FROM Login WHERE LoginName='" + name_field.getText().trim() + "'");
                        if (!rs.next())
                            JOptionPane.showMessageDialog(this, "账号不存在", "WARNING", JOptionPane.WARNING_MESSAGE);
                        else {
                            dispose();
                            JOptionPane.showMessageDialog(this, "欢迎使用学生管理系统", "WELCOME", JOptionPane.PLAIN_MESSAGE);
                            operationWin = new OperationWin("系统操作界面", rs.getString(1));
                            con.close();
                        }
                    }
                }
                catch(SQLException e){
                    JOptionPane.showMessageDialog(this,e,"WARNING",JOptionPane.WARNING_MESSAGE);
                    System.exit(0);
                }
            }
        } else if (event.getSource() == exit) {
            if (JOptionPane.showConfirmDialog(this, "是否退出", "提示", JOptionPane.YES_NO_CANCEL_OPTION) == JOptionPane.YES_OPTION) {
                dispose();
                interfaceWin = new InterfaceWin();
            }
        }
    }
}
